package redisdemo;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:application-redis.xml")
public class HashRedisTest {
    @Resource
    RedisTemplate redisTemplate;

    HashOperations  hashOperations;

    @Before
    public void load(){
        hashOperations = redisTemplate.opsForHash();
    }

    @Test
    //Boolean putIfAbsent(H key, HK hashKey, HV value);
    //仅当hashKey不存在时才设置散列hashKey的值。
    public void putIfAbsent(){
        hashOperations.putIfAbsent("man","name","湘湘");
    }

    @Test
    //void put(H key, HK hashKey, HV value);
    //设置散列hashKey的值
    public void put (){
        redisTemplate.delete("man");
//        hashOperations.putIfAbsent("man","name","湘湘");
        hashOperations.put("man","age",3);
        hashOperations.put("man","sex",0);
        System.out.println(hashOperations.entries("man"));
    }

    @Test
    //Long delete(H key, Object... hashKeys);
    //删除给定的哈希hashKeys
    public void delete(){
        System.out.println(hashOperations.delete("redisHash","name"));
        System.out.println(hashOperations.entries("redisHash"));
    }

    @Test
    //Boolean hasKey(H key, Object hashKey);
    //确定哈希hashKey是否存在
    public void hasKey(){
        Boolean aBoolean = hashOperations.hasKey("man", "name");
        System.out.println(aBoolean);
    }

    @Test
    //HV get(H key, Object hashKey);
    //从键中的哈希获取给定hashKey的值
    public void get(){
        System.out.println(hashOperations.get("man","name"));
    }

    @Test
    //List<HV> multiGet(H key, Collection<HK> hashKeys);
    //从哈希中获取给定hashKey的值
    public void multiGet(){
        List<Object> keys = new ArrayList<>();
        keys.add("name");
        keys.add("age");
        System.out.println(hashOperations.multiGet("man",keys));
    }

    @Test
    //Long increment(H key, HK hashKey, long delta);
    //通过给定的delta增加散列hashKey的值（整型）
    //使用GenericToStringSerializer、StringRedisSerializer序列化器，都可以使用increment方法Double increment(H key, HK hashKey, double delta);
    //通过给定的delta增加散列hashKey的值（浮点数）
    public void increment(){
        hashOperations.put("incrementHash","age","17");
        System.out.println(redisTemplate.boundHashOps("incrementHash").get("age"));//17
        System.out.println(hashOperations.increment("incrementHash","age",1));//18
        System.out.println(hashOperations.increment("incrementHash","age",1.1));//19.1
    }

    @Test
    //Set<HK> keys(H key);
    //获取key所对应的散列表的key
    public void keys(){
        Set man = hashOperations.keys("man");
        System.out.println(man);
    }

    @Test
    //Long size(H key);
    //获取key所对应的散列表的大小个数
    public void size(){
        System.out.println(hashOperations.size("man"));
    }

    @Test
    //List<HV> values(H key);
    //获取整个哈希存储的值根据密钥
    public void values(){
        System.out.println(hashOperations.values("man"));
    }

    //Cursor<Map.Entry<HK, HV>> scan(H key, ScanOptions options);
    //使用Cursor在key的hash中迭代，相当于迭代器。
    //复制代码
}
